package edu.henu.personnel.dao;

import edu.henu.personnel.entity.Talent;
import edu.henu.personnel.entity.Teacher;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @program: PersonnelProject
 * @description: 优秀教师人才Dao
 * @author: zhuyang
 */

//@Mapper
@Repository
public interface TeacherDao {

    /**
     * 通过人才列表来获取指定的
     * @param tid
     * @return 返回指定的优秀人才教师及其所在的组别
     */
    @Select("select * from t_teacher where tid = #{tid}")
    @Results({
            @Result(id = true, property = "tid", column = "tid"),
            @Result(property = "ttid", column = "ttid"),
            @Result(property = "talentName", column = "ttid", javaType = String.class,
                    one = @One(select = "edu.henu.personnel.dao.TalentDao.getTalentNameById", fetchType = FetchType.LAZY))
    })
    Teacher getTeacherByTid(@Param("tid") String tid);


    /**
     * 获取所有的优秀人才教师
     * @return
     */
    @Select("select tid, tname, ttid, tdate from t_teacher")
    @Results({
            @Result(id = true, property = "tid", column = "tid"),
            @Result(property = "ttid", column = "ttid"),
            @Result(property = "talentName", column = "ttid", javaType = String.class,
                    one = @One(select = "edu.henu.personnel.dao.TalentDao.getTalentNameById"))
    })
    List<Teacher> getAllTeacher();


    // 通过人才类别获取其中所有的优秀人才教师
    @Select("select * from t_teacher where ttid = #{ttid}")
    List<Teacher> getTeacherListByTtid(@Param("ttid") Integer ttid);



    @Select("select * from t_teacher where tname = #{tname}")
    @Results({
            @Result(id = true, property = "tid", column = "tid"),
            @Result(property = "ttid", column = "ttid"),
            @Result(property = "talentName", column = "ttid", javaType = Talent.class,
                    one = @One(select = "edu.henu.personnel.dao.TalentDao.getTalentInfoById", fetchType = FetchType.LAZY))
    })
    Teacher getTeacherByTname(@Param("tname") String tname);

    /**
     * 添加优秀教师人才
     * @param teacher
     * @return 添加成功放回true，否则返回false
     */
    @Insert("insert into t_teacher (tid, tname, tphoto, ttid, tintro, tsource, tdate, tbrowses) values" +
            "(#{tid}, #{tname}, #{tphoto}, #{ttid}, #{tintro}, #{tsource}, #{tdate}, #{tbrowses})")
    int insertTeacher(Teacher teacher);

    /**
     * 修改优秀教师人才的信息
     * @param teacher
     * @return 修改成功返回1，否则返回0
     */
    @Update("update t_teacher set tname=#{tname}," +
            "tphoto=#{tphoto}," +
            "ttid=#{ttid}," +
            "tintro=#{tintro}," +
            "tsource=#{tsource}," +
            "tdate=#{tdate}," +
            "tbrowses=#{tbrowses} " +
            "where tid=#{tid}")
    int updateTeacher(Teacher teacher);

    // 根据指定的tid删除优秀人才教师
    @Delete("delete from t_teacher where tid = #{tid}")
    int deleteTeacher(@Param("tid") String tid);

    // 增加teacher的访问量
    @Update("update t_teacher set tbrowses = tbrowses + 1 where tid = #{tid}")
    int addBrowses(String tid);

}
